package cn.vivi.recyclercomp;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class StatusHelper {

	private Context mContext;
	private ViewGroup mRootView;
	private View mViewEmpty;
	private View mViewEmptyInvalidNet;
	private View mViewEmptyErrorView;
	private View mViewEmptyRefreshing;
	private View mViewContent;


	public StatusHelper(Context context, ViewGroup rootView) {
		mContext = context;
		mRootView = rootView;
		addStatusViews();
	}


	private void addStatusViews() {
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		mRootView.addView(getEmptyView(), 0, params);
		mRootView.addView(getEmptyRefreshingView(), 1, params);
		mRootView.addView(getEmptyInvalidNetView(), 2, params);
		mRootView.addView(getDataContentView(), 3, params);
		mRootView.addView(getEmptyErrorView(), 4, params);

	}


	public void setDataContentView(View contentView) {
		mViewContent = contentView;
		mRootView.removeViewAt(3);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		mRootView.addView(mViewContent, 3, params);
		mRootView.invalidate();
	}


	public void setEmptyView(View emptyView) {
		mViewEmpty = emptyView;
		mRootView.removeViewAt(0);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		mRootView.addView(mViewEmpty, 0, params);
		mRootView.invalidate();
	}


	public void setEmptyInvalidNetView(View view) {
		mViewEmptyInvalidNet = view;
		mRootView.removeViewAt(2);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		mRootView.addView(mViewEmptyInvalidNet, 2, params);
		mRootView.invalidate();
	}


	public void setEmptyRefreshing(View view) {
		mViewEmptyRefreshing = view;
		mRootView.removeViewAt(1);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		mRootView.addView(mViewEmptyRefreshing, 1, params);
		mRootView.invalidate();
	}


	public void setEmptyErrorView(View view) {
		mViewEmptyErrorView = view;
		mRootView.removeViewAt(4);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		mRootView.addView(mViewEmptyErrorView, 4, params);
		mRootView.invalidate();
	}


	private View createDefaultEmptyView() {
		TextView tv = new TextView(mContext);
		tv.setText("Default Empty View");
		return tv;
	}


	private View createDefaultContentView() {
		TextView tv = new TextView(mContext);
		tv.setText("Default Content View");
		return tv;
	}


	private View createDefaultEmptyRefreshingView() {
		ProgressBar pb = new ProgressBar(mContext);
		pb.setIndeterminate(true);
		return pb;
	}


	private View createDefaultEmptyInvalidNetView() {
		TextView tv = new TextView(mContext);
		tv.setText("Default Empty Unreach Net View");
		return tv;
	}


	private View createDefaultEmptyErrorView() {
		TextView tv = new TextView(mContext);
		tv.setText("Default Empty Error View");
		return tv;
	}


	public View getEmptyView() {
		if (mViewEmpty == null) {
			mViewEmpty = createDefaultEmptyView();
		}
		return mViewEmpty;
	}


	public View getDataContentView() {
		if (mViewContent == null) {
			mViewContent = createDefaultContentView();
		}
		return mViewContent;
	}


	public View getEmptyRefreshingView() {
		if (mViewEmptyRefreshing == null) {
			mViewEmptyRefreshing = createDefaultEmptyRefreshingView();
		}
		return mViewEmptyRefreshing;
	}


	public View getEmptyInvalidNetView() {
		if (mViewEmptyInvalidNet == null) {
			mViewEmptyInvalidNet = createDefaultEmptyInvalidNetView();
		}
		return mViewEmptyInvalidNet;
	}


	public View getEmptyErrorView() {
		if (mViewEmptyErrorView == null) {
			mViewEmptyErrorView = createDefaultEmptyErrorView();
		}
		return mViewEmptyErrorView;
	}


	public void changeStatus(Status status) {
		// mRootView.removeAllViews();
		if (status == Status.EMPTY) {
			getEmptyView().setVisibility(View.VISIBLE);
		} else {
			getEmptyView().setVisibility(View.GONE);
		}

		if (status == Status.CONTENT) {
			getDataContentView().setVisibility(View.VISIBLE);
		} else {
			getDataContentView().setVisibility(View.GONE);
		}

		if (status == Status.EMPTY_REFRESHING) {
			getEmptyRefreshingView().setVisibility(View.VISIBLE);
		} else {
			getEmptyRefreshingView().setVisibility(View.GONE);
		}

		if (status == Status.EMPTY_INVALID_NEWWORK) {
			getEmptyInvalidNetView().setVisibility(View.VISIBLE);
		} else {
			getEmptyInvalidNetView().setVisibility(View.GONE);
		}

		if (status == Status.EMPTY_ERROR) {
			getEmptyErrorView().setVisibility(View.VISIBLE);
		} else {
			getEmptyErrorView().setVisibility(View.GONE);
		}
	}


	private View getViewOfStatus(Status status) {
		switch (status) {
		case EMPTY:
			return getEmptyView();

		case EMPTY_INVALID_NEWWORK:
			return getEmptyInvalidNetView();

		case EMPTY_REFRESHING:
			return getEmptyRefreshingView();
		case EMPTY_ERROR:
			return getEmptyErrorView();

		case CONTENT:
			return getDataContentView();

		default:
			return getEmptyView();
		}
	}

}
